package org.eredlab.g4.rif.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.sf.ehcache.Element;

import org.eredlab.g4.arm.service.MonitorService;
import org.eredlab.g4.arm.service.ResourceService;
import org.eredlab.g4.arm.util.ArmConstants;
import org.eredlab.g4.bmf.base.IDao;
import org.eredlab.g4.bmf.base.IReader;
import org.eredlab.g4.bmf.util.SpringBeanLoader;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.properties.PropertiesFactory;
import org.eredlab.g4.ccl.properties.PropertiesFile;
import org.eredlab.g4.ccl.properties.PropertiesHelper;
import org.eredlab.g4.ccl.util.G4Utils;
import org.eredlab.g4.ccl.util.GlobalConstants;
import org.springframework.context.ApplicationContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统启动监听器
 * 
 * @author XiongChun
 * @since 2010-04-13
 */
public class SystemInitListener implements ServletContextListener {
	private static Log log = LogFactory.getLog(SystemInitListener.class);
	private boolean success = true;
	private ApplicationContext wac = null;

	public void contextDestroyed(ServletContextEvent sce) {

	}

	public void contextInitialized(ServletContextEvent sce) {
		systemStartup(sce.getServletContext());
	}

	/**
	 * 应用平台启动
	 */
	private void systemStartup(ServletContext servletContext) {
	    PropertiesHelper pHelper = PropertiesFactory.getPropertiesHelper(PropertiesFile.G4);
	    String forceLoad = pHelper.getValue("forceLoad", ArmConstants.FORCELOAD_N);
		long start = System.currentTimeMillis();
		if (forceLoad.equalsIgnoreCase(ArmConstants.FORCELOAD_N)) {
			System.out.println("********************************************");
			System.out.println("易道系统集成与应用开发平台开始启动...");
			System.out.println("********************************************");
		}
		try {
			wac = SpringBeanLoader.getApplicationContext();
		} catch (Exception e) {
			success = false;
			e.printStackTrace();
		}
		if (success) {
			//清除托管Session
			MonitorService monitorService = (MonitorService)SpringBeanLoader.getSpringBean("monitorService");
			monitorService.deleteHttpSession(new BaseDto());
			try {
				initDbType();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		Dto dto = new BaseDto();
		dto.put("enabled", ArmConstants.EDITMODE_Y);
		if (success) {
			System.out.println("-------------------------------");
			System.out.println("系统开始启动代码表装载程序...");
			System.out.println("开始加载代码表...");
			ResourceService resourceService = (ResourceService) SpringBeanLoader.getSpringBean("resourceService");
			IReader g4Reader = (IReader) SpringBeanLoader.getSpringBean("g4Reader");
			List codeList = null;
			try {
				//codeList = resourceService.getCodeList(dto).getDefaultAList();
				codeList = g4Reader.queryForList("getCodeViewList");
				/**
				 * huangxy 2011-04-26
				 * 
				 * 新增代码表写入缓存
				*/
				for (int i = 0; i < codeList.size(); i++) {
					Dto codeDto = (BaseDto) codeList.get(i);
					Element element = WebUtils.getParamElement(codeDto.getAsString("field"));
					if(element == null)	
					{
						Dto bto = new BaseDto();
						List lst = new ArrayList();
						lst.add(codeDto);
						bto.setDefaultAList(lst);						
						//WebUtils.putParamValue(new Element(codeDto.getAsString("field")+codeDto.getAsString("code"),codeDto));	
						WebUtils.putParamValue(new Element(codeDto.getAsString("field"),bto));
					}
					else
					{
						((Dto)element.getObjectValue()).getDefaultAList().add(codeDto);
						WebUtils.putParamValue(element);
					}
				}
				System.out.println("代码表加载成功!");
			} catch (Exception e) {
				success = false;
				System.out.println("代码表加载失败!");
				e.printStackTrace();
			}
			servletContext.setAttribute("EACODELIST", codeList);

		}
		if (success) {
			System.out.println("-------------------------------");
			System.out.println("系统开始启动全局参数表装载程序...");
			System.out.println("开始加载全局参数表...");
			List paramList = null;
			try {
				ResourceService resourceService = (ResourceService) SpringBeanLoader.getSpringBean("resourceService");
				IReader g4Reader = (IReader) SpringBeanLoader.getSpringBean("g4Reader");
				//paramList = resourceService.getParamList(dto).getDefaultAList();
				paramList = g4Reader.queryForList("getParamList");
				/**
				 * huangxy 2011-04-26
				 * 新增全局参数缓存到ehcache中
				 */
				System.out.println("开始缓存全局参数表......");
				for (int i = 0; i < paramList.size(); i++) {
					Dto paramDto = (BaseDto) paramList.get(i);
					WebUtils.putParamValue(new Element(paramDto.getAsString("paramkey"),paramDto.getAsString("paramvalue")));					
				}
				System.out.println("完成缓存全局参数表.......");
				System.out.println("全局参数表加载成功!");
			} catch (Exception e) {
				success = false;
				System.out.println("全局参数表加载失败!");
				e.printStackTrace();
			}
			servletContext.setAttribute("EAPARAMLIST", paramList);
		}
		long timeSec = (System.currentTimeMillis() - start) / 1000;
		System.out.println("********************************************");
		if (success) {
			System.out.println("易道系统集成与应用开发平台启动成功[" + G4Utils.getCurrentTime() + "]");
			System.out.println( "启动总耗时: " + timeSec/60+ "分 " + timeSec%60 + "秒 ");
		} else {
			System.out.println("易道系统集成与应用开发平台启动失败[" + G4Utils.getCurrentTime() + "]");
			System.out.println( "启动总耗时: " + timeSec/60+ "分" + timeSec%60 + "秒");
		}
		System.out.println("********************************************");
	}
	/**
	 * 识别JDBC驱动类型
	 * 
	 * @throws SQLException
	 */
	private void initDbType() throws SQLException {
		IDao g4Dao = (IDao) SpringBeanLoader.getSpringBean("g4Dao");
		Connection connection = g4Dao.getConnection();
		if (connection.getMetaData().getDatabaseProductName().toLowerCase().indexOf("ora") > -1) {
			System.setProperty("eRedg4.JdbcType", "oracle");
		} else if (connection.getMetaData().getDatabaseProductName().toLowerCase().indexOf("mysql") > -1) {
			System.setProperty("eRedg4.JdbcType", "mysql");
		} else {
			if (log.isErrorEnabled()) {
				log.error(GlobalConstants.Exception_Head + "G4平台目前还不支持你使用的数据库产品.如需获得支持,请和我们联系!");
			}
			System.exit(0);
		}
	}
}
